package com.example.yichuangyun.mvvm.ui.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ItemCompanyBinding;
import com.example.yichuangyun.model.company.CompanyDataModel;
import com.example.yichuangyun.mvvm.ui.activity.ImproveCompanyInfoActivity;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.JumpIntent;
import com.example.yichuangyun.utils.TextUtilTools;
import com.example.yichuangyun.utils.UiUtils;

import java.util.ArrayList;
import java.util.List;

public class SearchCompanyAdapter extends BaseQuickAdapter<CompanyDataModel.CompanyModel, DataBindingHolder<ItemCompanyBinding>> {

    private final Context context;
    private String keywords;
    /**
     * 0默认企业列表、1我的收藏
     */
    private int flag;
    private double centerLatitude, centerLongitude;

    public SearchCompanyAdapter(Context context) {
        this.context = context;
    }

    public SearchCompanyAdapter(Context context, int flag) {
        this.context = context;
        this.flag = flag;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public void setLocation(double centerLatitude, double centerLongitude) {
        this.centerLatitude = centerLatitude;
        this.centerLongitude = centerLongitude;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemCompanyBinding> holder, int position, @Nullable CompanyDataModel.CompanyModel item, @NonNull List<?> payloads) {
        super.onBindViewHolder(holder, position, item, payloads);
        ItemCompanyBinding binding = holder.getBinding();
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position, item);
        } else {
            if (flag == 0) {
                if (null != item) {
                    binding.ivLike.setImageResource(item.getCollectState() == 0 ? R.mipmap.icon_like_sel : R.mipmap.icon_like_def);
                    binding.tvLike.setText(item.getCollectState() == 0 ? context.getString(R.string.company_like) : context.getString(R.string.company_cancel_like));
                    binding.tvLike.setTextColor(item.getCollectState() == 0 ? context.getColor(R.color.color_426FFF) : context.getColor(R.color.color_8D9AA9));
                }
            }
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemCompanyBinding> itemCompanyBindingDataBindingHolder, int i, @Nullable CompanyDataModel.CompanyModel model) {
        ItemCompanyBinding binding = itemCompanyBindingDataBindingHolder.getBinding();
        binding.setCompanyModel(model);
        if (null != model) {
            Glide.with(context).load(model.getLogoUrl()).placeholder(R.mipmap.icon_company_def_logo).into(binding.ivCompanyLogo);
            if (!TextUtils.isEmpty(keywords)) {
                if (TextUtils.isEmpty(model.getName())) {
                    SpannableStringBuilder spannableStringBuilder = TextUtilTools.highlight(model.getName(), keywords);
                    binding.tvCompanyName.setText(spannableStringBuilder);
                }
            } else {
                binding.tvCompanyName.setText(CommonUtil.setText2(model.getName()));
            }
            if (flag == 1) {
                binding.ivLike.setImageResource(R.mipmap.icon_like_def);
                binding.tvLike.setText(context.getString(R.string.company_cancel_like));
                binding.tvLike.setTextColor(context.getColor(R.color.color_8D9AA9));
            } else {
                binding.ivLike.setImageResource(model.getCollectState() == 0 ? R.mipmap.icon_like_sel : R.mipmap.icon_like_def);
                binding.tvLike.setText(model.getCollectState() == 0 ? context.getString(R.string.company_like) : context.getString(R.string.company_cancel_like));
                binding.tvLike.setTextColor(model.getCollectState() == 0 ? context.getColor(R.color.color_426FFF) : context.getColor(R.color.color_8D9AA9));
            }

            final List<String> tags = new ArrayList<>();
            if (!TextUtils.isEmpty(model.getNewStatusCode())) {
                tags.add(UiUtils.getManagementStatus(model.getNewStatusCode()));
            }
            if (model.isWskh()) {
                tags.add(context.getString(R.string.wskh));
            }
            if (model.getPayTaxeState() == 1) {
                tags.add(context.getString(R.string.a_nsr));
            }
            if (model.getTelCount() > 0) {
                tags.add(context.getString(R.string.tel_count, model.getTelCount()));
            }
            binding.tagCloudView.setTags(tags);
            binding.tvDistance.setVisibility(flag == 1 ? View.GONE : View.VISIBLE);
            binding.tvDistance.setText(CommonUtil.getDistance(AMapUtils.calculateLineDistance(
                    new LatLng(centerLatitude, centerLongitude),
                    new LatLng(Constant.isSearch ? model.getLatitude() : model.getGdLatitude(), Constant.isSearch ? model.getLongitude() : model.getGdLongitude()))));
//            Drawable drawable = context.getResources().getDrawable(R.mipmap.icon_nav, null);
//            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            binding.tvRegAddress.setText(context.getString(R.string.reg_address_mao, CommonUtil.setText3(model.getAddress())));
            binding.tvReportAddress.setText(context.getString(R.string.report_address_mao, CommonUtil.setText3(model.getReportAddress())));
            if (TextUtils.isEmpty(model.getAddress()) && TextUtils.isEmpty(model.getReportAddress()) && !TextUtils.isEmpty(model.getLatestAddress())) {
                binding.tvRegAddress.setVisibility(View.GONE);
                binding.tvReportAddress.setText(context.getString(R.string.address_mao, CommonUtil.setText3(model.getLatestAddress())));
//                binding.tvReportAddress.setCompoundDrawables(null, null, TextUtils.isEmpty(model.getLatestAddress()) ? null : drawable, null);
            } else {
                binding.tvRegAddress.setVisibility(View.VISIBLE);
                binding.tvRegAddress.setText(context.getString(R.string.reg_address_mao, CommonUtil.setText3(model.getAddress())));
                binding.tvReportAddress.setText(context.getString(R.string.report_address_mao, CommonUtil.setText3(model.getReportAddress())));
//                binding.tvRegAddress.setCompoundDrawables(null, null, TextUtils.isEmpty(model.getAddress()) ? null : drawable, null);
//                binding.tvReportAddress.setCompoundDrawables(null, null, TextUtils.isEmpty(model.getReportAddress()) ? null : drawable, null);
            }

            if (model.getCallRecordResp() != null) {
                CompanyDataModel.CompanyModel.CallCountModel callCountModel = model.getCallRecordResp();
                binding.tvContactCount.setVisibility(View.VISIBLE);
                if (callCountModel.getDay() == 0) { // 查询当天
                    binding.tvContactCount.setText(context.getString(R.string.today_contacts_count, callCountModel.getNumber()));
                } else if (callCountModel.getDay() > 0 && callCountModel.getDay() <= 3) {
                    binding.tvContactCount.setText(context.getString(R.string.three_day_contacts_count, callCountModel.getNumber()));
                } else {
                    binding.tvContactCount.setText(context.getString(R.string.week_contacts_count, callCountModel.getNumber()));
                }
            } else {
                binding.tvContactCount.setVisibility(View.GONE);
            }

            binding.tvRegAddress.setEnabled(!TextUtils.isEmpty(model.getAddress()));
            binding.tvReportAddress.setEnabled(!TextUtils.isEmpty(model.getReportAddress()) || !TextUtils.isEmpty(model.getLatestAddress()));

            binding.tvRegAddress.setOnLongClickListener(view -> {
                if (!TextUtils.isEmpty(model.getAddress())) {
                    CommonUtil.copyText(context, model.getAddress());
                }
                return true;
            });

            binding.tvReportAddress.setOnLongClickListener(view -> {
                if (!TextUtils.isEmpty(model.getReportAddress())) {
                    CommonUtil.copyText(context, model.getReportAddress());
                } else {
                    if (!TextUtils.isEmpty(model.getLatestAddress())) {
                        CommonUtil.copyText(context, model.getLatestAddress());
                    }
                }
                return true;
            });

//            binding.tvRegAddress.setOnClickListener(view -> {
//                Bundle bundle = new Bundle();
//                bundle.putString(Constant.nameKey, flag == 1 ? model.getCompanyName() : model.getName());
//                bundle.putString(Constant.addressKey, model.getAddress());
//                bundle.putDouble(Constant.latitudeKey, model.getGdLatitude());
//                bundle.putDouble(Constant.longitudeKey, model.getGdLongitude());
//                JumpIntent.jump((Activity) context, MapNavigationActivity.class, bundle);
//            });

//            binding.tvReportAddress.setOnClickListener(view -> {
//                Bundle bundle = new Bundle();
//                bundle.putString(Constant.nameKey, flag == 1 ? model.getCompanyName() : model.getName());
//                bundle.putString(Constant.addressKey, TextUtils.isEmpty(model.getReportAddress()) ? model.getLatestAddress() : model.getReportAddress());
//                bundle.putDouble(Constant.latitudeKey, model.getGdLatitude());
//                bundle.putDouble(Constant.longitudeKey, model.getGdLongitude());
//                JumpIntent.jump((Activity) context, MapNavigationActivity.class, bundle);
//            });

            binding.llInfo.setOnClickListener(view -> {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.eidKey, model.getEid());
                bundle.putString(Constant.creditNoKey, model.getCreditNo());
                JumpIntent.jump((Activity) context, ImproveCompanyInfoActivity.class, bundle);
            });

        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemCompanyBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_company, viewGroup);
    }
}
